package com.esun.service.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.CollectionUtils;
import com.esun.common.constant.SystemsConstant;
import com.esun.common.enums.Message;
import com.esun.common.utils.FileUtils;
import com.esun.common.utils.GuidUtils;
import com.esun.common.utils.MessageUtils;
import com.esun.dao.das.DatabaseLogDas;

import com.esun.api.request.DatabaseLogRequestDto;
import com.esun.api.response.DatabaseLogResponseDto;
import com.esun.dao.eo.DatabaseLogEo;
import com.esun.dao.eo.ExampleEo;
import com.esun.dao.eo.PageEo;
import com.esun.dao.eo.SortEo;

import com.esun.service.service.DatabaseLogService;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.*;

/**
 * (DatabaseLog)表服务实现类
 *
 * @author baron.wubo
 * @since 2021-10-13 10:39:43
 */
@Service
public class DatabaseLogServiceImpl implements DatabaseLogService {
    @Resource
    private DatabaseLogDas databaseLogDas;
    @Value("${file.path:/}")
    String filePath;

    @Override
    public PageInfo<DatabaseLogResponseDto> getDatabaseLogByPage(DatabaseLogRequestDto requestDto) {
        List<DatabaseLogResponseDto> responseDtoList = new ArrayList<>();
        PageEo pageEo = PageEo.builder().pageIndex(requestDto.getPageIndex()).pageSize(requestDto.getPageSize()).build();
        DatabaseLogEo databaseLogEo = DatabaseLogEo.builder().build();
        BeanUtils.copyProperties(requestDto,databaseLogEo);
        List<SortEo> sortList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(requestDto.getSortList())){
            requestDto.getSortList().forEach((sortRequestDto -> {
                sortList.add(SortEo.builder().sortParam(sortRequestDto.getSortParam()).ascending(sortRequestDto.getAscending()).build());
            }));
        }
        PageInfo<DatabaseLogEo> pageInfo = databaseLogDas.queryPageBySort(databaseLogEo,pageEo, sortList);
        List<DatabaseLogEo> eoList = pageInfo.getList();
        for (DatabaseLogEo eo : eoList){
            DatabaseLogResponseDto responseDto = new DatabaseLogResponseDto();
            BeanUtils.copyProperties(eo,responseDto);
            responseDtoList.add(responseDto);
        }
        PageInfo<DatabaseLogResponseDto> responseDtoPageInfo = new PageInfo<>(responseDtoList);
        responseDtoPageInfo.setTotal(pageInfo.getTotal());
        return responseDtoPageInfo;
    }

    @Override
    public DatabaseLogResponseDto getLogErrorTotal() {
        DatabaseLogEo databaseLogEo = new DatabaseLogEo();
        databaseLogEo.setStatus(1);
        int total = databaseLogDas.errorTotal(databaseLogEo);
        DatabaseLogResponseDto databaseLogResponseDto = new DatabaseLogResponseDto();
        databaseLogResponseDto.setTotal(total);
        return databaseLogResponseDto;
    }

    @Override
    public List<DatabaseLogResponseDto> addDatabaseLog(List<DatabaseLogRequestDto> requestDtoList) {
        List<DatabaseLogEo> eoList = new ArrayList<>();
        List<DatabaseLogResponseDto> responseDtoList = new ArrayList<>();
        for (DatabaseLogRequestDto requestDto : requestDtoList){
            DatabaseLogEo databaseLogEo = DatabaseLogEo.builder().build();
            BeanUtils.copyProperties(requestDto,databaseLogEo);
            DatabaseLogResponseDto responseDto = new DatabaseLogResponseDto();
            BeanUtils.copyProperties(databaseLogEo,responseDto);
            String code = Message.QUERY_SUCCESS.getCode();
            responseDto.setErrorCode(code);
            responseDto.setMsg(databaseLogEo.getUserName()+ MessageUtils.getInfoByCode(code));
            responseDtoList.add(responseDto);
            eoList.add(databaseLogEo);
        }
        //syncExampleToMongo(eoList);
        for (DatabaseLogResponseDto responseDto : responseDtoList) {
            if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return responseDtoList;
            }
        }
        if (!CollectionUtils.isEmpty(eoList)){
            databaseLogDas.insertBatch(eoList);
        }
        return responseDtoList;
    }

    @Override
    public List<DatabaseLogResponseDto> updateDatabaseLog(List<DatabaseLogRequestDto> requestDtoList) {
        List<DatabaseLogEo> eoList = new ArrayList<>();
        List<DatabaseLogResponseDto> responseDtoList = new ArrayList<>();
        for (DatabaseLogRequestDto requestDto : requestDtoList){
            DatabaseLogEo databaseLogEo = DatabaseLogEo.builder().build();
            BeanUtils.copyProperties(requestDto,databaseLogEo);
            DatabaseLogResponseDto responseDto = new DatabaseLogResponseDto();
            BeanUtils.copyProperties(databaseLogEo,responseDto);
            String code = Message.QUERY_SUCCESS.getCode();
            responseDto.setErrorCode(code);
            responseDto.setMsg(databaseLogEo.getUserName()+ MessageUtils.getInfoByCode(code));
            responseDtoList.add(responseDto);
            eoList.add(databaseLogEo);
        }
        for (DatabaseLogResponseDto responseDto : responseDtoList) {
            if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return responseDtoList;
            }
        }
        for (DatabaseLogEo eo: eoList) {
            databaseLogDas.update(eo);
        }
        return responseDtoList;
    }

    @Override
    public List<DatabaseLogResponseDto> deleteDatabaseLog(List<DatabaseLogRequestDto> requestDtoList) {
        List<DatabaseLogEo> eoList = new ArrayList<>();
        List<DatabaseLogResponseDto> responseDtoList = new ArrayList<>();
        for (DatabaseLogRequestDto requestDto : requestDtoList){
            DatabaseLogEo databaseLogEo = DatabaseLogEo.builder().build();
            BeanUtils.copyProperties(requestDto,databaseLogEo);
            DatabaseLogResponseDto responseDto = new DatabaseLogResponseDto();
            BeanUtils.copyProperties(databaseLogEo,responseDto);
            String code = Message.QUERY_SUCCESS.getCode();
            responseDto.setErrorCode(code);
            responseDto.setMsg(databaseLogEo.getUserName()+ MessageUtils.getInfoByCode(code));
            responseDtoList.add(responseDto);
            eoList.add(databaseLogEo);
        }
        for (DatabaseLogResponseDto responseDto : responseDtoList) {
            if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return responseDtoList;
            }
        }
        if(!CollectionUtils.isEmpty(eoList)){
            databaseLogDas.deleteByIdBatch(eoList);
        }
        return responseDtoList;
    }

    @Override
    public List<DatabaseLogResponseDto> importDatabaseLog(Workbook workbook) {
        return null;
    }

    @Override
    public void exportDatabaseLog(DatabaseLogRequestDto requestDto) {
        DatabaseLogEo databaseLogEo = DatabaseLogEo.builder().build();
        BeanUtils.copyProperties(requestDto,databaseLogEo);
        List<DatabaseLogEo> eoList = databaseLogDas.queryAll(databaseLogEo);
        String programUrl = System.getProperty("user.dir");
        String fileName = programUrl.concat(filePath).concat(GuidUtils.getGuid()).concat(SystemsConstant.HEADQUARTERS);
        EasyExcel.write(fileName, ExampleEo.class).sheet("数据库日志").doWrite(eoList);
        FileUtils fileUtils = new FileUtils();
        fileUtils.downLoad(fileName);
    }

    @Override
    public void downloadTemplate(String path) {

    }
}
